﻿@using TamoCRM.Core
@using TamoCRM.Domain.StatusMap
@model TamoCRM.Web.Mvc.Areas.Admin.Models.ContactLevelInfos.ContactLevelInfoModel
@{
    ViewBag.Title = "Thông tin học viên";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutSingle.cshtml";
}
@{
    var genders = ViewBag.Genders;
    var lstGenders = new SelectList(genders, "Code", "Name");

    var submitedProfileTypes = ViewBag.SubmitedProfileTypes;
    var lstsubmitedProfileTypes = new SelectList(submitedProfileTypes, "Code", "Name");

    var appointments = ViewBag.Appointments;
    var lstAppointments = new SelectList(appointments, "Code", "Name");

    var statusCares = ViewBag.StatusCares;
    var lstStatusCares = new SelectList(statusCares, "Id", "Name");

    var statusMaps = ((IEnumerable<StatusMapInfo>)ViewBag.StatusMaps)
        .Where(c => c.Name.ToLower() != "chưa gọi")
        .Where(c => c.StatusMapType == (int)StatusMapUsedBy.Tvts)
        .Where(c => c.LevelId == 0 || c.LevelId == Model.ContactInfo.LevelId)
        .OrderBy(c => c.StatusCareId);
    var lstStatusMaps = new SelectList(statusMaps, "Id", "Name");

    var lumpeds = ViewBag.Lumpeds;
    var lstLumpeds = new SelectList(lumpeds, "Id", "Name");

    var latchs = ViewBag.Latchs;
    var lstLatchs = new SelectList(latchs, "Id", "Name");

    var schools = ViewBag.Schools;
    var lstSchools = new SelectList(schools, "SchoolId", "Name");
}
<style>
    .widget-body label {
        padding-top: 0px;
    }
</style>

<div class="page-content">
    <div class="page-header">
        <h1>Thông tin học viên
        </h1>
    </div>
    <!-- /.page-header -->

    <div class="row">
        <div class="form-group">
            <div class="col-sm-12" id="alert">
            </div>
        </div>
        <div class="col-xs-12">
            @using (Html.BeginForm("Edit", "ContactLevelInfo", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
                @Html.HiddenFor(x => x.ContactInfo.Id)
                @Html.HiddenFor(x => x.ContactInfo.LevelId)
                @Html.HiddenFor(x => x.ContactInfo.StatusId)
                @Html.HiddenFor(x => x.ContactInfo.SchoolId)
                @Html.HiddenFor(x => x.ContactInfo.CallCount)
                @Html.HiddenFor(x => x.ContactLevelInfo.CollaboratorName)
                <div class="widget-box" id="divL1">
                    <div class="widget-header">
                        <h4>Thông tin L1</h4>
                    </div>
                    <div class="widget-body">
                        <div class="widget-main">
                            <div class="row">
                                <div class="col-xs-1" style="width: 11%;">
                                    <label>Họ Tên</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="clearfix">
                                        @Html.TextBoxFor(x => x.ContactInfo.Fullname, new { @style = "width: 100%", @required = "required", error = "Họ và tên rỗng" })
                                    </div>
                                </div>
                                <div class="col-xs-1">
                                    <label>Giới tính</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    @Html.DropDownListFor(x => x.ContactInfo.Gender, lstGenders, new { @style = "width: 100%" })
                                </div>
                                <div class="col-xs-1" style="width: 14%;">
                                    <label>Ngày sinh</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="clearfix">
                                        @Html.TextBoxFor(x => x.Birthday, new { @style = "width: 100%" })
                                    </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-1" style="width: 11%;">
                                    <label>SĐT 1</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="input-group clearfix">
                                        <span class="input-group-addon" style="padding: 0px;">
                                            <button id="btnCall1" type="button" class="btn-primary icon-phone" style="height: 26px; width: 30px;"></button>
                                        </span>
                                        @Html.TextBoxFor(x => x.Mobile1, new { @style = "width: 100%", @readonly = "readonly" })
                                    </div>
                                </div>
                                <div class="col-xs-1">
                                    <label>SĐT 2</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="input-group clearfix">
                                        <span class="input-group-addon" style="padding: 0px;">
                                            <button id="btnCall2" type="button" class="btn-primary icon-phone" style="height: 26px; width: 30px;"></button>
                                        </span>
                                        @Html.TextBoxFor(x => x.Mobile2, new { @style = "width: 100%", @pattern = "^(?!0)((\\d{3}-\\d{3}-\\d{4,5})|\\d{9,10})$", error = "Số điện thoại không đúng định dạng" })
                                    </div>
                                </div>
                                <div class="col-xs-1" style="width: 14%;">
                                    <label>SĐT 3</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="input-group clearfix">
                                        <span class="input-group-addon" style="padding: 0px;">
                                            <button id="btnCall3" type="button" class="btn-primary icon-phone" style="height: 26px; width: 30px;"></button>
                                        </span>
                                        @Html.TextBoxFor(x => x.Tel, new { @style = "width: 100%", @pattern = "^(?!0)((\\d{3}-\\d{3}-\\d{4,5})|\\d{9,10})$", error = "Số điện thoại không đúng định dạng" })
                                    </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-1" style="width: 11%;">
                                    <label>Email 1</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="icon-envelope" style="width: 11px;"></i>
                                        </span>
                                        @Html.TextBoxFor(x => x.ContactInfo.Email, new { @style = "width: 100%", @type = "email", error = "Email không đúng định dạng" })
                                    </div>
                                </div>
                                <div class="col-xs-1">
                                    <label>Email 2</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="input-group">
                                        <span class="input-group-addon">
                                            <i class="icon-envelope" style="width: 11px;"></i>
                                        </span>
                                        @Html.TextBoxFor(x => x.ContactInfo.Email2, new { @style = "width: 100%", @type = "email", error = "Email không đúng định dạng" })
                                    </div>
                                </div>
                                <div class="col-xs-1" style="width: 14%;">
                                    <label>Gộp</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="clearfix">
                                        @Html.DropDownListFor(x => x.ContactLevelInfo.LumpedCareId, lstLumpeds, new { @style = "width: 100%" })
                                    </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-1" style="width: 11%;">
                                    <label>Địa chỉ liên hệ</label>
                                </div>
                                <div class="col-xs-6" style="width: 52.25%;">
                                    <div class="clearfix">
                                        @Html.TextBoxFor(x => x.ContactInfo.Address, new { @style = "width: 100%" })
                                    </div>
                                </div>
                                <div class="col-xs-1" style="width: 14%;">
                                    <label>Trường chăm sóc</label>
                                </div>
                                <div class="col-xs-3" style="width: 22%;">
                                    <div class="clearfix">
                                        @Html.DropDownListFor(x => x.ContactInfo.SchoolCare, lstSchools, new { @style = "width: 100%" })
                                    </div>
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <button id="btnUpdateL1" type="button" class="btn-primary" style="float: right; margin: 10px 20px 0 0;" onclick="return updateL1();">Cập nhật thông tin L1</button>
                                <div class="clear-fix"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="space-6"></div>
                <div class="widget-box" id="divL2">
                    <div class="widget-header">
                        <h4>Thông tin L2</h4>
                    </div>
                    <div class="widget-body">
                        <div class="widget-main">
                            <div class="row">
                                <div class="col-xs-2">
                                    <label>Đã đi làm </label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.HaveJob)
                                </div>
                                <div class="col-xs-1">
                                    <label>Làm gì? </label>
                                </div>
                                <div class="col-xs-9">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.JobTitle, new { @style = "width: 100%", disabled = "disabled" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-4">
                                    <label>Đã từng học liên thông, ĐH, VB2 </label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.HaveLearnTransfer)
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-2">
                                    <label>Đã tốt nghiệp? </label>
                                </div>
                                <div class="col-xs-5">
                                    @Html.CheckBoxListFor(model => model.PostedEducationLevels.EducationLevelIds, model => model.AllEducationLevels, source => source.EducationLevelId, source => source.Name, model => model.SelectedEducationLevels)
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-4">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.EducationLevelNotes, new { @style = "width: 100%" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-2">
                                    &nbsp;
                                </div>
                                <div class="col-xs-1">
                                    <label>Trường</label>
                                </div>
                                <div class="col-xs-4">
                                    @Html.TextBoxFor(model => model.ContactLevelInfo.SchoolGrad, new { @style = "width: 100%" })
                                </div>
                                <div class="col-xs-1">
                                    <label>Ngành </label>
                                </div>
                                <div class="col-xs-4">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.MajorGrad, new { @style = "width: 100%" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-5">
                                    <label>Có muốn lấy tiếp bằng cử nhân? </label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.WantStudy, new { @style = "display: none" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-2">
                                    <label>Ngành </label>
                                </div>
                                <div class="col-xs-5">
                                    @Html.CheckBoxListFor(model => model.PostedMajors.MajorIds, model => model.AllMajors, source => source.MajorId, source => source.Name, model => model.SelectedMajors)
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-4">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.WantStudyNotes, new { @style = "width: 100%" })
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="space-6"></div>
                <div class="widget-box" id="divL3">
                    <div class="widget-header">
                        <h4>Thông tin L3 - L5B</h4>
                    </div>
                    <div class="widget-body">
                        <div class="widget-main">
                            <div class="row">
                                <div class="col-xs-2">
                                    <label>Lịch hẹn?</label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.Appointment)
                                </div>
                                <div class="col-xs-2">
                                    @Html.TextBoxFor(x => x.AppointmentTime, new { disabled = "disabled" })
                                </div>
                                <div class="col-xs-2">
                                    @Html.DropDownListFor(x => x.ContactLevelInfo.ApointmentType, lstAppointments, new { disabled = "disabled" })
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-5">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.ApointmentNotes, new { @style = "width: 100%" })
                                </div>
                            </div>
                            <div id="divL4">
                                <div class="space-4"></div>
                                <div class="row">
                                    <div class="col-xs-6">
                                        <label>Nộp phiếu đăng ký (F30)?</label>
                                        @Html.CheckBoxFor(x => x.ContactLevelInfo.SubmitedForm)
                                    </div>
                                    <div class="col-xs-1">
                                        <label>Ghi chú </label>
                                    </div>
                                    <div class="col-xs-5">
                                        @Html.TextBoxFor(x => x.ContactLevelInfo.SubmitedFormNotes, new { @style = "width: 100%" })
                                    </div>
                                </div>
                            </div>
                            <div id="divL5">
                                <div class="space-4"></div>
                                <div class="row">
                                    <div class="col-xs-2">
                                        <label>Nộp phí xét tuyển?</label>
                                        @Html.CheckBoxFor(x => x.ContactLevelInfo.FeeReview)
                                    </div>
                                    <div class="col-xs-2">
                                        <label style="width: 25%">Gộp</label>
                                        @Html.DropDownListFor(x => x.ContactLevelInfo.LumpedFeeId, lstLumpeds, new { @style = "width: 72%", disabled = "disabled" })
                                    </div>
                                    <div class="col-xs-2">
                                        <label style="width: 25%">Chốt</label>
                                        @Html.DropDownListFor(x => x.ContactLevelInfo.LatchId, lstLatchs, new { @style = "width: 72%", disabled = "disabled" })
                                    </div>
                                    <div class="col-xs-1">
                                        <label>Ghi chú </label>
                                    </div>
                                    <div class="col-xs-5">
                                        @Html.TextBoxFor(x => x.ContactLevelInfo.FeeReviewNotes, new { @style = "width: 100%" })
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="space-6"></div>
                <div class="widget-box" id="divL6">
                    <div class="widget-header">
                        <h4>Thông tin L6 - L8</h4>
                    </div>
                    <div class="widget-body">
                        <div class="widget-main">
                            <div class="row">
                                <div class="col-xs-3">
                                    <label>Đủ điều kiện xét tuyển?</label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.AcceptedReview)
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-8">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.AcceptedReviewNotes, new { @style = "width: 100%" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-3">
                                    <label>Đã nộp học phí?</label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.FeeEdu)
                                </div>

                                <div class="col-xs-1">
                                    <label>Gộp </label>
                                </div>
                                <div class="col-xs-2">
                                    @Html.DropDownListFor(x => x.ContactLevelInfo.LumpedAdmissionId, lstLumpeds, new { @style = "width: 100%", disabled = "disabled" })
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-5">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.FeeEduNotes, new { @style = "width: 100%" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-3">
                                    <label>Hồ sơ?</label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.SubmitedProfile)
                                </div>
                                <div class="col-xs-1">
                                    <label>Thông tin</label>
                                </div>
                                <div class="col-xs-2">
                                    @Html.DropDownListFor(x => x.ContactLevelInfo.SubmitedProfileType, lstsubmitedProfileTypes)
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-5">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.SubmitedProfileNotes, new { @style = "width: 100%" })
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
                <div class="space-6"></div>
                <div class="widget-box" id="divL9">
                    <div class="widget-header">
                        <h4>Thông tin L9</h4>
                    </div>
                    <div class="widget-body">
                        <div class="widget-main">
                            <div class="row">
                                <div class="col-xs-3">
                                    <label>Hoàn thiện hồ sơ</label>
                                    @Html.CheckBoxFor(x => x.ContactLevelInfo.Completed)
                                </div>
                                <div class="col-xs-1">
                                    <label>Thời gian </label>
                                </div>
                                <div class="col-xs-2">
                                    @Html.TextBoxFor(x => x.CompletedTime, new { @style = "width: 100%", disabled = "disabled" })
                                </div>
                                <div class="col-xs-1">
                                    <label>Ghi chú </label>
                                </div>
                                <div class="col-xs-5">
                                    @Html.TextBoxFor(x => x.ContactLevelInfo.CompletedNotes, new { @style = "width: 100%" })
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="space-6"></div>
                <div class="widget-box">
                    <div class="widget-header">
                        <h4>Nội dung & kết quả cuộc gọi</h4>
                    </div>
                    <div class="widget-body">
                        <div class="widget-main">
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-1" style="width: 9.3333%;">
                                    <label>Lịch gọi lại</label>
                                </div>
                                <div class="col-xs-3">
                                    @Html.TextBoxFor(x => x.RecallTime, string.Empty, new { @class = "input-small", @style = "width:50%;" })
                                    <span class="input-append bootstrap-timepicker">
                                        @Html.TextBoxFor(x => x.RecallTime24h, string.Empty, new { @class = "input-small" })
                                        <span class="add-on"><i class="icon-time"></i></span>
                                    </span>
                                </div>
                                <div class="col-xs-2" style="width: 15.666666%;">
                                    <label>Trạng thái gọi </label>
                                </div>
                                <div class="col-xs-2">
                                    @Html.HiddenFor(x => x.CallHistoryId, string.Empty)
                                    @Html.DropDownListFor(x => x.ContactInfo.StatusMapId, lstStatusMaps, new { @style = "width: 100%;" })
                                </div>
                                <div class="col-xs-2">
                                    <label>Trạng thái chăm sóc</label>
                                </div>
                                <div class="col-xs-2">
                                    @Html.DropDownList("ContactInfo_StatusCareId", lstStatusCares, new { @style = "width: 100%;", onfocus = "this.defaultIndex=this.selectedIndex;", onchange = "this.selectedIndex=this.defaultIndex;" })
                                </div>
                            </div>
                            <div class="space-4"></div>
                            <div class="row">
                                <div class="col-xs-1" style="width: 9.3333%;">
                                    <label>Nội dung </label>
                                </div>
                                <div class="col-xs-11" style="width: 90.666666%;">
                                    @Html.TextAreaFor(x => x.CallCenterInfo, new { @style = "width: 100%", @required = "required", error = "Nội dung cuộc gọi không được rỗng" })
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="space-6"></div>
                <button id="btnSubmit" type="submit" class="btn-primary" style="float: right;" onclick="return submitForm();">Cập nhật</button>
                <div class="clearfix"></div>
                
                <div class="space-6"></div>
                <table id="grid-table"></table>
                <div id="grid-pager"></div>
            }
        </div>
        <!-- /.col -->
    </div>
    <!-- /.row -->
</div>
<script src="/Content/themes/ace/assets/js/jquery.form.js"></script>
<script src="/Content/themes/ace/assets/js/jquery.validate.min.js"></script>
<script src="/Content/themes/ace/assets/js/additional-methods.min.js"></script>


<script type="text/javascript">
    var grid_selector = "#grid-table";
    var callCount = '@Model.ContactInfo.CallCount';
    var url = 'admin/api/callhistory/getbycontactId?contactid={0}&userid={1}';
    var heades = ['Id', 'Thời gian gọi', 'Trạng thái', 'Trạng thái gọi', 'Thời gian gọi lại', 'Nội dung', 'Ghi âm', 'CallType'];
    var model = [
        { name: 'CallHistoryId', index: 'CallHistoryId', editable: false, sorttype: 'string', editoptions: { disabled: true }, width: '0px' },
        { name: 'CallTime', index: 'CallTime', editable: false, sorttype: 'string', width: '70px', formatter: 'date', formatoptions: { srcformat : 'Y-m-dTH:i:s', newformat : 'd/m/Y H:i' } },
        { name: 'StatusCareId', index: 'StatusCareId', editable: false, width: '120px', formatter: statusCareFomatter },
        { name: 'StatusMapId', index: 'StatusMapId', editable: false, width: '120px', formatter: statusMapFomatter },
        { name: 'RecallTime', index: 'RecallTime', editable: false, sorttype: 'string', width: '50px', formatter: 'date', formatoptions: { newformat: 'd/m/Y' } },
        { name: 'CallCenterInfo', index: 'CallCenterInfo', editable: false, sorttype: 'string', width: '100px' },
        { name: 'LinkRecord', index: 'LinkRecord', editable: false, align: 'center', width: '100px', formatter: listenFomatter },
        { name: 'CallType', index: 'CallType', editable: false, align: 'center', editoptions: { disabled: true }, width: '0px' }
    ];

    function checkLevel2() {
        var checkEducationLevel = false;
        var elements = document.getElementsByName('PostedEducationLevels.EducationLevelIds');
        for (var i = 0; i < elements.length; i++) {
            if ($(elements[i]).is(':checked')) {
                checkEducationLevel = true;
                break;
            }
        }

        var checkSchool = false;
        if ($('#ContactLevelInfo_MajorGrad').val() != '') checkSchool = true;

        var checkMajor = false;
        elements = document.getElementsByName('PostedMajors.MajorIds');
        for (var i = 0; i < elements.length; i++) {
            if ($(elements[i]).is(':checked')) {
                checkMajor = true;
                break;
            }
        }
        
        var check = checkEducationLevel && checkSchool && checkMajor;
        if (!check) {
            var error = 'Thông tin L2 đang bị thiếu, thông tin gồm:';
            if (!checkEducationLevel || !checkSchool) error += "<br />- Thông tin trường tốt nghiệp, ngành";
            if (!checkMajor) error += "<br />- Thông tin ngành muốn học tiếp";
            alertError(error);
        }
        return check;
    }

    function checkLevel3() {
        var checkAppointmentTime = false;
        if ($('#AppointmentTime').val() != '') checkAppointmentTime = true;
        
        var check = checkAppointmentTime;
        if (!check) {
            var error = 'Thông tin L3 đang bị thiếu, thông tin gồm:';
            if (!checkAppointmentTime) error += "<br />- Thông tin lịch hẹn (ngày hẹn)";
            alertError(error);
        }
        return check;
    }

    function checkLevel5B() {
        var checkSubmitedForm = false;
        if ($('#ContactLevelInfo_SubmitedForm').is(':checked')) checkSubmitedForm = true;

        var checkFeeReview = false;
        if ($('#ContactLevelInfo_FeeReview').is(':checked')) checkFeeReview = true;

        var checkAddress = false;
        if ($('#ContactInfo_Address').val() != '') checkAddress = true;

        var checkBirthday = false;
        if ($('#Birthday').val() != '') checkBirthday = true;
        
        var check = checkSubmitedForm && checkFeeReview && checkAddress && checkBirthday;
        if (!check) {
            var error = 'Thông tin L5B đang bị thiếu, thông tin gồm:';
            if (!checkSubmitedForm) error += "<br />- Nộp phiếu đăng ký (F30)";
            if (!checkFeeReview) error += "<br />- Nộp phí xét tuyển";
            if (!checkAddress) error += "<br />- Địa chỉ liên hệ";
            if (!checkBirthday) error += "<br />- Ngày sinh";
            alertError(error);
        }
        return check;
        
    }
    
    function checkLevel8() {
        var checkFeeEdu = false;
        if ($('#ContactLevelInfo_FeeEdu').is(':checked')) checkFeeEdu = true;
        
        var check = checkFeeEdu;
        if (!check) {
            var error = 'Thông tin L8 đang bị thiếu, thông tin gồm:';
            if (!checkFeeEdu) error += "<br />- Đã nộp học phí";
            alertError(error);
        }
        return check;
    }

    function checkLevel9() {
        var checkCompleted = false;
        if ($('#ContactLevelInfo_Completed').is(':checked')) checkCompleted = true;
        
        var checkCompletedTime = false;
        if ($('#CompletedTime').val() != '') checkCompletedTime = true;
        
        var check = checkCompleted && checkCompletedTime;
        if (!check) {
            var error = 'Thông tin L9 đang bị thiếu, thông tin gồm:';
            if (!checkCompleted) error += "<br />- Hoàn thiện hồ sơ";
            if (!checkCompletedTime) error += "<br />- Thời gian hoàn thiện hồ sơ";
            alertError(error);
        }
        return check;
    }

    function checkStatusDontCall() {
        return $('#ContactInfo_StatusCareId').val() == '@((int)StatusCareType.DontCall)';
    }
    
    function checkStatusUnknown() {
        return $('#ContactInfo_StatusCareId').val() == '@((int)StatusCareType.UnKnown)';
    }
    
    function checkLevelNext(levelId) {
        if (levelId == '@((int)LevelType.L1)' ||
            levelId == '@((int)LevelType.L8)' || 
            levelId == '@((int)LevelType.L9)')
            return true;
        return false;
    }
    
    function submitForm() {
        var array = @Html.Raw(Json.Encode(((IEnumerable<StatusMapInfo>)ViewBag.StatusMaps).Select(c => new
               {
                   Id = c.Id,
                   Name = c.Name,
                   LevelIdNext = c.LevelIdNext,
                   StatusCareId = c.StatusCareId
               })));
        var levelIdNext = 0;
        var statusMapId = $('#ContactInfo_StatusMapId').val();
        for (var i = 0; i < array.length; i++) {
            if (array[i].Id == statusMapId) {
                levelIdNext = array[i].LevelIdNext;
                if (levelIdNext == '@((int)LevelType.L2)' && !checkLevel2()) {
                    return false;
                }
                
                if (levelIdNext == '@((int)LevelType.L3)' && !checkLevel2() && !checkLevel3()) {
                    return false;
                }
                
                if (levelIdNext == '@((int)LevelType.L5B)' && !checkLevel2() && !checkLevel3() && !checkLevel5B()) {
                    return false;
                }
                
                if (levelIdNext == '@((int)LevelType.L8)' && !checkLevel2() && !checkLevel3() && !checkLevel5B() && !checkLevel8()) {
                    return false;
                }
                
                if (levelIdNext == '@((int)LevelType.L9)' && !checkLevel2() && !checkLevel3() && !checkLevel5B() && !checkLevel8() && !checkLevel9()) {
                    return false;
                }
                break;
            }
        }
        if (callCount >= 2 && checkStatusUnknown()) {
            return true;
        }

        if (!checkStatusDontCall() && !checkLevelNext(levelIdNext)) {
            if ($('#RecallTime').val() == '') {
                alertError('Thông tin ngày hẹn không được rỗng');
                return false;
            }
        }
        return true;
    }

    $(document).ready(function () {
        isViewMode();
        turnOnInput();
        displayLevel();
        customValidate();
        changeStatusMap();
        changeColorStatusMap();
        
        $('#Birthday').datepicker({ format: 'dd/mm/yyyy', autoclose: true, dateonly:true, endDate: "+0d"  });
        $('#RecallTime').datepicker({ format: 'dd/mm/yyyy', autoclose: true, dateonly:true, startDate: "+0d"  });
        $('#CompletedTime').datepicker({ format: 'dd/mm/yyyy', autoclose: true, dateonly:true, startDate: "+0d"  });
        $('#AppointmentTime').datepicker({ format: 'dd/mm/yyyy', autoclose: true, dateonly:true, startDate: "+0d"  });
        $('#RecallTime24h').timepicker();
        
        setupGridTable("#grid-table", "", url.format('@ViewBag.Id', '@ViewBag.UserId'), "admin/api/callhistory/edit", "Id", "Lịch sử các cuộc gọi", heades, model, "", 1000, '350px');
        
        // Call
        $("#btnCall1").click(function (e) {
            callMobile('Mobile1', 'btnCall1');
        });
        $("#btnCall2").click(function (e) {
            callMobile('Mobile2', 'btnCall2');
        });
        $("#btnCall3").click(function (e) {
            callMobile('Tel', 'btnCall3');
        });

        // Change disable
        $("#divL2 :input").change(function () {
            if (this.id == 'ContactLevelInfo_HaveJob') {
                if ($(this).is(':checked'))
                    $("#ContactLevelInfo_JobTitle").removeAttr("disabled");
                else
                    $("#ContactLevelInfo_JobTitle").attr("disabled", "disabled");
            }
        });
        $("#divL3 :input").change(function () {
            if (this.id == 'ContactLevelInfo_Appointment') {
                if ($(this).is(':checked')) {
                    $("#AppointmentTime").removeAttr("disabled");
                    $("#ContactLevelInfo_ApointmentType").removeAttr("disabled");
                } else {
                    $("#AppointmentTime").attr("disabled", "disabled");
                    $("#ContactLevelInfo_ApointmentType").attr("disabled", "disabled");
                }
            }
            
            if (this.id == 'ContactLevelInfo_FeeReview') {
                if ($(this).is(':checked')) {
                    $("#ContactLevelInfo_LatchId").removeAttr("disabled");
                    $("#ContactLevelInfo_LumpedFeeId").removeAttr("disabled");

                } else {
                    $("#ContactLevelInfo_LatchId").attr("disabled", "disabled");
                    $("#ContactLevelInfo_LumpedFeeId").attr("disabled", "disabled");
                }
            }
        });
        $("#divL6 :input").change(function () {
            if (this.id == 'ContactLevelInfo_FeeEdu') {
                if ($(this).is(':checked')) {
                    $("#ContactLevelInfo_LumpedAdmissionId").removeAttr("disabled");
                } else {
                    $("#ContactLevelInfo_LumpedAdmissionId").attr("disabled", "disabled");
                }
            }

            if (this.id == 'ContactLevelInfo_SubmitedProfile') {
                if ($(this).is(':checked')) {
                    $("#ContactLevelInfo_SubmitedProfileType").removeAttr("disabled");
                } else {
                    $("#ContactLevelInfo_SubmitedProfileType").attr("disabled", "disabled");
                }
            }
        });
        $("#divL9 :input").change(function () {
            if (this.id == 'ContactLevelInfo_Completed') {
                if ($(this).is(':checked')) {
                    $("#CompletedTime").removeAttr("disabled");
                } else {
                    $("#CompletedTime").attr("disabled", "disabled");
                }
            }
        });
        $('#ContactInfo_StatusMapId').change(function () {
            changeStatusMap();
        });
        $('#ContactInfo_StatusCareId').attr('readonly', 'readonly');
        
        // Change Lumped
        $("#ContactLevelInfo_LumpedFeeId").change(function () {
            var lumpedId = $(this).val();
            var url = "/admin/api/latch/GetByLumpedIdIsClose?lumpedId=" + lumpedId + "&isclose=0";
            $.ajax({
                url: url,
                type: 'GET',
                success: function (data) {
                    if (data == null) return;
                    var options = '';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].Id + '">' + data[i].Name + '</option>';

                    }
                    $('#ContactLevelInfo_LatchId').empty().append(options);
                }
            });
        });
    });

    function displayLevel() {
        // Level
        var levelId = '@Model.ContactInfo.LevelId';
        switch (levelId) {
            case '@((int)LevelType.L1)':
            case '@((int)LevelType.L2)':
                $('#divL3 h4').html('Thông tin L3');
                $("#divL4").css("display", "none");
                $("#divL5").css("display", "none");
                $("#divL6").css("display", "none");
                $("#divL9").css("display", "none");
                break;
            case '@((int)LevelType.L3)':
            case '@((int)LevelType.L4)':
                $("#divL6").css("display", "none");
                $("#divL9").css("display", "none");
                break;
            case '@((int)LevelType.L8)':
                $('#ContactLevelInfo_LatchId').attr('readonly', 'readonly');
                $('#ContactLevelInfo_LatchId').attr('onfocus', 'this.defaultIndex=this.selectedIndex;');
                $('#ContactLevelInfo_LatchId').attr('onchange', 'this.selectedIndex=this.defaultIndex;');
                    
                $('#ContactLevelInfo_LumpedFeeId').attr('readonly', 'readonly');
                $('#ContactLevelInfo_LumpedFeeId').attr('onfocus', 'this.defaultIndex=this.selectedIndex;');
                $('#ContactLevelInfo_LumpedFeeId').attr('onchange', 'this.selectedIndex=this.defaultIndex;');
                    
                $('#ContactLevelInfo_LumpedAdmissionId').attr('readonly', 'readonly');
                $('#ContactLevelInfo_LumpedAdmissionId').attr('onfocus', 'this.defaultIndex=this.selectedIndex;');
                $('#ContactLevelInfo_LumpedAdmissionId').attr('onchange', 'this.selectedIndex=this.defaultIndex;');
                break;
        }
        
        // CallCount
        if (callCount >= 3) {
            $("#btnCall1").attr("disabled", "disabled");
            $("#btnCall2").attr("disabled", "disabled");
            $("#btnCall3").attr("disabled", "disabled");
        }
            
        // LumpedCare
        var lumpedCareId = '@(Model.ContactLevelInfo == null ? 0 : Model.ContactLevelInfo.LumpedCareId)';
        if (lumpedCareId != null && lumpedCareId > 0) {
            $('#ContactLevelInfo_LumpedCareId').attr('readonly', 'readonly');
            $('#ContactLevelInfo_LumpedCareId').attr('onfocus', 'this.defaultIndex=this.selectedIndex;');
            $('#ContactLevelInfo_LumpedCareId').attr('onchange', 'this.selectedIndex=this.defaultIndex;');
        }
    }

    function turnOnInput() {
        $("#btnCall1").removeAttr("disabled");
        $("#btnCall2").removeAttr("disabled");
        $("#btnCall3").removeAttr("disabled");

        if ($('#ContactLevelInfo_HaveJob').is(':checked'))
            $("#ContactLevelInfo_JobTitle").removeAttr("disabled");
        
        if ($('#ContactLevelInfo_Appointment').is(':checked')) {
            $("#AppointmentTime").removeAttr("disabled");
            $("#ContactLevelInfo_ApointmentType").removeAttr("disabled");
        } else {
            $("#ContactLevelInfo_ApointmentType").attr("disabled", "disabled");
            $("#AppointmentTime").attr("disabled", "disabled");
        }

        if ($('#ContactLevelInfo_SubmitedForm').is(':checked')) {
            $("#ContactLevelInfo_SubmitedProfileType").removeAttr("disabled");
        } else {
            $("#ContactLevelInfo_SubmitedProfileType").attr("disabled", "disabled");
        }

        if ($('#ContactLevelInfo_FeeReview').is(':checked')) {
            $("#ContactLevelInfo_LatchId").removeAttr("disabled");
            $("#ContactLevelInfo_LumpedFeeId").removeAttr("disabled");
        } else {
            $("#ContactLevelInfo_LatchId").attr("disabled", "disabled");
            $("#ContactLevelInfo_LumpedFeeId").attr("disabled", "disabled");
        }

        if ($('#ContactLevelInfo_SubmitedProfile').is(':checked')) {
            $("#ContactLevelInfo_LumpedAdmissionId").removeAttr("disabled");
        } else {
            $("#ContactLevelInfo_LumpedAdmissionId").attr("disabled", "disabled");
        }

        if ($('#ContactLevelInfo_Completed').is(':checked')) {
            $("#CompletedTime").removeAttr("disabled");
        } else {
            $("#CompletedTime").attr("disabled", "disabled");
        }
    }

    function updateL1() {
        $.ajax({
            type: 'POST',
            url: '/admin/api/contact/updatel1',
            data: {
                id: $('#ContactInfo_Id').val(),
                name: $('#ContactInfo_Fullname').val(),
                gender: $('#ContactInfo_Gender').val(),
                birthday: $('#Birthday').val(),
                mobile1: $('#Mobile1').val(),
                mobile2: $('#Mobile2').val(),
                tel: $('#Mobile3').val(),
                email1: $('#ContactInfo_Email').val(),
                email2: $('#ContactInfo_Email2').val(),
                lumpedId: $('#ContactLevelInfo_LumpedCareId').val(),
                address: $('#ContactInfo_Address').val(),
                schoolId: $('#ContactInfo_SchoolCare').val(),
            },
            success: function (message) {
                $("#alert").addClass("alert alert-block alert-success");
                $("#alert").html(message);
            },
            error: function (request, status, ex) {
            }
        }); 
    }
        
    function callMobile(name, button) {
        if ($('#' + name).val() == '') {
            alertError("Số điện thoại không được rỗng");
            return;
        }
        if ($('#' + name)[0].checkValidity()) {
            $("#" + button).attr("disabled", "disabled");

            var id = '@ViewBag.Id';
                var mobile = $('#' + name).val();
                var newUrl = '/admin/api/callhistory/call?id={0}&mobile={1}';
                $.ajax({
                    type: 'GET',
                    url: newUrl.format(id, mobile),
                    success: function (data) {
                        alertSuccess("Gọi điện thành công.");
                        if (data != null && data.CallHistoryId != null)
                            $('#CallHistoryId').val(data.CallHistoryId);

                        jQuery(grid_selector).jqGrid().trigger("reloadGrid");
                    },
                    error: function (request, status, ex) {
                        $('#' + name).removeAttr("disabled");
                        $("#" + button).removeAttr("disabled");
                        alertError("Gọi điện không thành công.");
                    }
                });
            }
        }
    
        function changeStatusMap() {
            var array = @Html.Raw(Json.Encode(((IEnumerable<StatusMapInfo>)ViewBag.StatusMaps).Select(c => new { Id = c.Id, Name = c.Name, StatusCareId = c.StatusCareId })));
            for (var i = 0; i < array.length; i++) {
                if ($('#ContactInfo_StatusMapId').val() == array[i].Id) {
                    var statusCareId = array[i].StatusCareId;
                    $('#ContactInfo_StatusCareId').val(statusCareId);
                }
            }
        }
    
        function changeColorStatusMap() {
            var array = @Html.Raw(Json.Encode(((IEnumerable<StatusMapInfo>)ViewBag.StatusMaps).Select(c => new { Id = c.Id, Name = c.Name, StatusCareId = c.StatusCareId })));
        $("#ContactInfo_StatusCareId option").each(function()
        {
            var id = $(this).val();
            if (id == 1) {
                $(this).css("background-color", "YellowGreen"); 
            } else  if (id == 2) {
                $(this).css("background-color", "Yellow");                
            } else  if (id == 3) {
                $(this).css("background-color", "Orange");                
            } else  if (id == 4) {
                $(this).css("background-color", "Violet");                
            }
            
            for (var i = 0; i < array.length; i++) {
                $("#ContactInfo_StatusMapId option").each(function() {
                    if ($(this).val() == array[i].Id) {
                        var id = array[i].StatusCareId;
                        if (id == 1) {
                            $(this).css("background-color", "YellowGreen");
                        } else if (id == 2) {
                            $(this).css("background-color", "Yellow");
                        } else if (id == 3) {
                            $(this).css("background-color", "Orange");
                        } else if (id == 4) {
                            $(this).css("background-color", "Violet");
                        }
                    }
                });
            }
        });
    }
    
    function isViewMode() {
        var mode = '@ViewBag.IsView';
        if (mode == 1) {
            $('#btnCall1').css('display', 'none');
            $('#btnCall2').css('display', 'none');
            $('#btnCall3').css('display', 'none');
            $('#btnSubmit').css('display', 'none');
        }
    }

    var message = '@ViewBag.Message';
    if (message == null || message == '') {
        $("#alert").removeClass();
        $("#alert").html('');
    }
    else {
        $("#alert").addClass("alert alert-block alert-success");
        $("#alert").html(message);
        if ('@ViewBag.Close' == '1')
            window.close();
    }
    
    function requestFullScreen(element) {
        // Supports most browsers and their versions.
        var requestMethod = element.requestFullScreen || element.webkitRequestFullScreen || element.mozRequestFullScreen || element.msRequestFullScreen;

        if (requestMethod) { // Native full screen.
            requestMethod.call(element);
        } else if (typeof window.ActiveXObject !== "undefined") { // Older IE.
            var wscript = new ActiveXObject("WScript.Shell");
            if (wscript !== null) {
                wscript.SendKeys("{F11}");
            }
        }
    }
</script>
